/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.ibm.staf.STAFException;
import com.ibm.staf.STAFHandle;
import com.ibm.staf.STAFResult;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import module.MyModule;

/**
 *
 * @author think
 */
public class ServerUtil {

    ConfigManager configManager;

    @Inject
    public ServerUtil(ConfigManager config) {
        configManager = config;
    }

    public boolean createServerSideProjectArchive() {
        try {
            STAFHandle handle = new STAFHandle("SU");
            File archive = new File(configManager.getProjectArchiveName());
            if (archive.exists()) {
                archive.delete();
            }
            handle = new STAFHandle(configManager.getZipHandleName());
            STAFResult result = handle.submit2(configManager.getServerHostName(), "zip", "add zipfile " + "\"" + configManager.getProjectArchiveName().replace("\\", "/") +
                    "\" directory \"" + configManager.getProjectPath().replace("\\", "/") + "\" relativeto \"" + configManager.getProjectPath().replace("\\", "/") + "\" recurse");
            handle.unRegister();
            if (result.rc == 0) {
                return true;
            } else {
                System.out.println(result.result);
            }
        } catch (STAFException ex) {
            Logger.getLogger(VMMachine.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;

    }

    public boolean isBuildInReleaseDirectory(String buildName){
        return ifDirContainsFile(configManager.getInstallerPath(),buildName);
    }

    public boolean isReleaseDirAccesable(){
        return true;
    }
    
    public List searchFiles(String pattern, String dir) {
		List<File> result = new ArrayList<File>();
		File file = new File(dir);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for(int i=0;i<files.length;i++){
				List set = searchFiles(pattern, files[i].getAbsolutePath());
				Iterator it = set.iterator();
				while(it.hasNext()){
					File f = (File)it.next();
					result.add(f);
				}
                set.clear();
			}
		} else {
            String fileName = file.getAbsolutePath();
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(fileName);
			if (m.find()) {
				result.add(file);
			}
		}
		return result;
	}

    private boolean ifDirContainsFile(String dir, String fileName) {
        File currentDir = new File(dir);
        if (!currentDir.exists()) {
            return false;
        }
        String[] allFiles = currentDir.list();
        for (int i = 0; i < allFiles.length; i++) {
            if (allFiles[i].toLowerCase().endsWith(fileName.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args){
        Injector injector = Guice.createInjector(new MyModule());
        ServerUtil util = injector.getInstance(ServerUtil.class);
        List zipFiles = util.searchFiles(".*zip$", "Y:\\UEP\\1.5");
        Iterator it = zipFiles.iterator();
        while(it.hasNext()){
            File name = (File)it.next();
            System.out.println(name.getAbsoluteFile());
        }

    }
}
